package com.jjty.dataGenerator.utils;



import com.jjty.dataGenerator.exception.ServiceException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Author: huangyulong
 * @createTime: 2023-04-26 15:31
 * @Description:
 */
public class TimeUtils {

    public static String getFormat(String format){
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(new Date());
    }

    public static String getFormat(String format,Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }


    public static Date toDate(String format,String str) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(str);
        } catch (ParseException e) {
            throw new ServiceException("日期类型转换异常：目标格式："+format+" 处理日期："+str);
        }
    }

    /**
     * 根据年月日返回日期
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static Date getDate(int year, Integer month, Integer day) {
        String strMonth = month < 10?"0"+month:""+month;
        String strDay = day < 10?"0"+day:""+day;
        return toDate("yyyyMMdd",""+year+strMonth+strDay);
    }


    /**
     * 日期增长
     * @param date 日期
     * @param amount 数量
     * @param calendarType 单位
     * @return
     */
    public static Date addDate(Date date,int amount,int calendarType){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendarType,amount);
        return calendar.getTime();
    }

    public static Date getCurrentFormatDate(String format) throws ServiceException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        String s = dateFormat.format(new Date());
        try{
            return dateFormat.parse(s);
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    public static Integer getYear(Date date){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 查询一周的第几天
     * 周一返回1
     * @param date
     * @return
     */
    public static Integer getDayOfWeek(Date date){
        if(date == null){
            date = new Date();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        int weekNum = calendar.get(Calendar.DAY_OF_WEEK);
        //日 一 二 三 四 五 六
        //1  2 3  4  5 6  7
        //处理成周一作为起始日，并且起始值为1
        return (weekNum - 1) == 0?7:weekNum - 1;
    }

    public static Date formatData(String format,Date date) throws ServiceException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        String s = dateFormat.format(date);
        try {
            return dateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 判断两个日期是否是同一月份
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameMonth(Date date1,Date date2){

        // 将Date对象转换为Calendar对象
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);

        // 获取两个日期的年份和月份
        int year1 = calendar1.get(Calendar.YEAR);
        int month1 = calendar1.get(Calendar.MONTH);
        int year2 = calendar2.get(Calendar.YEAR);
        int month2 = calendar2.get(Calendar.MONTH);

        // 判断两个日期是否在同一个月
        return (year1 == year2) && (month1 == month2);
    }

    /**
     * 获取本月的最后一天
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(Date date){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        // 将日历设置为本月的第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        // 将日历向前滚动到下个月的第一天，然后回退一天，即为当前月的最后一天
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        // 获取当月的最后一天
        return calendar.getTime();
    }
}
